package com.example.mall.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.Mapper.UserMapper;
import com.example.mall.security.entity.User;
import com.example.mall.util.ResultVo;
import com.example.mall.service.UserService;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author ：Dongdequan
 * @date ：Created in 2022/5/26 9:24
 * @modified By：
 */

@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> userList() {
        return userMapper.selectList(new QueryWrapper<>());
    }


public ResultVo login(String username, String password) {
    long userId = 1L;
    QueryWrapper queryWrapper = new QueryWrapper<User>().eq("username",username);
    User user = userMapper.selectOne(queryWrapper);
    if(user != null && user.getPassword().equals(password)) {
        long nowTime = System.currentTimeMillis();
        String token = Jwts.builder()
                .setHeaderParam("alg", "HS256")
                .setHeaderParam("typ", "JWT")
                .setSubject("JWT")
                .setIssuer(String.valueOf(userId))
                .setExpiration(new Date(nowTime + ResultVo.EXPIRE_TIME)) // 过期时间
                .claim("userId", userId) // 往Payload中添加自定义的key和value
                .signWith(ResultVo.SECRET_KEY) // 使用secretKey进行签名
                .compact(); // 生成JWT字符串
        Object jwt = token;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("token",jwt);
        jsonObject.put("username",user.getUsername());
        jsonObject.put("createdTime",user.getCreatedTime());
        return ResultVo.success(jsonObject);
    }else {
        return ResultVo.paramError("账号或者密码错误！");
    }
}

    public User selectLoginInfoByUserId(Long userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public ResultVo userInfo() {

        return new ResultVo().success(userMapper.selectOne(new QueryWrapper<User>().eq("id",1)));
    }

    @Override
    public ResultVo deleteUser(Long userId, Integer id) {
        User user = userMapper.selectById(id);
        if(user != null){
            if(userId.equals(id)){
                user.setIsDelete(1);
                return new ResultVo().success(userMapper.updateById(user));
            }
        }
        return new ResultVo().success("权限不足！");
    }

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        return null;
    }
}
